Equation
- ARMA model section (testarma.prg)
ARMA model section
The following subroutine generates a table of model selection criteria containing
the the permutation of ARMA models upto AR order scalar_ar and MA order up to order scalar_ma..
' subroutine that estimates all arma models up to specified lags
'
' usage: call arma(ser1, group1, scalar_ar, scalar_ma, tab_name)
'
' where
'
' ser1: name of dependent variable series
' group1: name of group of exogenous regressors (should include constant, if desired)
' scalar_ar: integer for highest AR order
' scalar_ma: integer for highest MA order
' tab_name: name for stored table
'
' the program stores the AIC, SC, and HQ information criteria in a table named {%tabname}
subroutine arma5(series y, group x, scalar ar_order, scalar ma_order, string %tabname)
' set upper limit lags
!pmax=ar_order
!qmax=ma_order
' declare test equation
equation eq_test
' declare table to store information criteria
table((!pmax+1)*(!qmax+1)+2,4) {%tabname}
setcolwidth({%tabname},1,11)
setcolwidth({%tabname},2,15)
setcolwidth({%tabname},3,15)
setcolwidth({%tabname},4,15)
{%tabname}(1,1) = "ARMA order"
{%tabname}(1,2) = "Akaike"
{%tabname}(1,3) = "Schwarz"
{%tabname}(1,4) = "Hannan-Quinn"
setline({%tabname},2)
' loop through every combination of arma lags
!row = 3
for !p=0 to !pmax
for !q=0 to !qmax
' build up ar terms
if !p=0 then
%1 = " "
else
for !i=1 to !p
%1 = %1 + "ar(" + @str(!i) + ") "
next
endif
' build up ma terms
if !q=0 then
%1 = %1 + " "
else
for !i=1 to !q
%1 = %1 + "ma(" + @str(!i) + ") "
next
endif
' estimate model
eq_test.ls(m=500,c=1e-5) y x {%1}
' store output in table
freeze(tab_{!p}{!q}) eq_test.output
%order = @str(!p) + "," + @str(!q)
{%tabname}(!row,1) = %order
{%tabname}(!row,2) = eq_test.@aic
{%tabname}(!row,3) = eq_test.@sc
{%tabname}(!row,4) = eq_test.@hq
' test for mininum noise model
if !row=3 then
!min_aic = eq_test.@aic
!min_sc = eq_test.@sc
!min_hq = eq_test.@hq
!order_aic = !row
!order_sc = !row
!order_hq = !row
else
if eq_test.@aic < !min_aic then
!min_aic = eq_test.@aic
!order_aic = !row
endif
if eq_test.@sc < !min_sc then
!min_sc = eq_test.@sc
!order_sc = !row
endif
if eq_test.@hq < !min_hq then
!min_hq = eq_test.@hq
!order_hq = !row
endif
endif
' clear string
%1 = " "
%order = " "
!row = !row+1
' delete table if not necessary
' delete tab_{!p}{!q}
next
next
' indicate best model in table
%aic = "*" + {%tabname}(!order_aic,2)
{%tabname}(!order_aic,2) = %aic
%sc = "*" + {%tabname}(!order_sc,3)
{%tabname}(!order_sc,3) = %sc
%hq = "*" + {%tabname}(!order_hq,4)
{%tabname}(!order_hq,4) = %hq
setline({%tabname},(!pmax+1)*(!qmax+1)+3)
{%tabname}((!pmax+1)*(!qmax+1)+4,1) = " * indicates best model"
endsub
The following program calls the above subroutine.
' program to find best arma order
include arma5.prg
' create workfile
workfile testarma a 1869 1987
!maxAR=3
!maxMA=3
' fill series
series gnp
gnp.fill -.173, -.2, -.237, -.093, -.066, -.092, -.103, -.048, -.006, .052, .111, .196, .195, .212, .204, .203, .19, .211, .22, .189, .204, .231, .26, .309, .274, .239, .299, .306, .345, .361, .4, .422, .481, .479, .511, .49, .523, .594, .603, .53, .577, .609, .614, .636, .659, .616, .668, .727, .709, .76, .758, .728, .685, .728, .835, .845, .855, .904, .9, .899, .953, .841, .753, .598, .57, .638, .715, .836, .879, .827, .894, .957, 1.099, 1.231, 1.358, 1.414, 1.387, 1.219, 1.183, 1.206, 1.194, 1.26, 1.323, 1.342, 1.363, 1.333, 1.381, 1.385, 1.384, 1.364, 1.401, 1.406, 1.416, 1.456, 1.481, 1.518, 1.565, 1.611, 1.627, 1.662, 1.68, 1.667, 1.69, 1.737, 1.785, 1.772, 1.752, 1.795, 1.839, 1.877, 1.894, 1.879, 1.894, 1.862, 1.889, 1.941, 1.959, 1.975, 1.993
' call subroutine
group x
x.add c
%tabname = "armaout"
call arma5(gnp, x, !maxAR, !maxMA, %tabname)
show {%tabname}
^Top